import 'dart:io';

import 'package:flu_framework/common/base/BaseWidget.dart';
import 'package:flu_framework/common/widgets/dialog_utils.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:image_picker/image_picker.dart';

class ImagePickerPage extends BaseWidget {
  const ImagePickerPage({super.key});

  @override
  BaseWidgetState<BaseWidget> getState() => _ImagePickerPageState();
}

class _ImagePickerPageState extends BaseWidgetState<ImagePickerPage> {

  ImagePicker _imagePicker = ImagePicker();
  XFile? _xFile;

  @override
  String setPageTitle() => "图片选择";
  
  @override
  Widget buildContent() {
    return Center(
      child: Column(
        children: [
          _xFile == null?
          ClipOval(child: Image.asset("images/default_head_icon.png",width: 80,height: 80,fit: BoxFit.fill,)):
          ClipOval(child: Image.file(File(_xFile!.path),width: 80,height: 80,fit: BoxFit.fill,),),
          ElevatedButton(
            onPressed: (){
              DialogUtils.showBottomPictureDialog(context, (pic){
                if(pic == SelectPictureType.PIC_CAMERA) {
                  _takePhoto();
                }else if(pic == SelectPictureType.PIC_GALLERY) {
                  _openGallery();
                }
              });
            },
            child: Text("替换图片"),
          )

        ],
      ),
    );
  }

  _takePhoto() async {
    XFile? pickedFile = await _imagePicker.pickImage(source: ImageSource.camera,maxHeight: 500,maxWidth: 600);
    if(pickedFile != null) {
      print(pickedFile.path);
      print(File(pickedFile.path));
      setState(() {
        _xFile = pickedFile;
      });
    }
  }

  _openGallery() async {
    XFile? pickedFile = await _imagePicker.pickImage(source: ImageSource.gallery,maxWidth: 600,maxHeight: 500);
    if(pickedFile != null) {
      print(pickedFile.path);
      print(File(pickedFile.path));
      setState(() {
        _xFile = pickedFile;
      });
    }
  }
}
